<?php
class CatalogsController extends AppController {

	var $name = 'Catalogs';

	function index() {
		$this->Catalog->recursive = 0;
		$this->set('catalogs', $this->paginate());
		$this->layout = 'mylayout';
	}

	function view($id = null, $id2=null) {
		
		$this->layout= 'trangchu';
		if (!$id) {
			$this->Session->setFlash(__('Invalid catalog', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('catalog', $this->Catalog->read(null, $id));
		$this->set('id',$id);
		if(!$id2){
			$data = $this->Post->find('all', array(
											'conditions' => array(	'Post.catalog_id' => $id, 
																	'Post.vip' => 1
																	),
											'order' => array('Post.created' => 'DESC'),
			));
			$this->set('data', $data);
		}
		else{	$data = $this->Post->find('all', array(
											'conditions' => array(	'Post.catalog_id' => $id, 
																	'Post.vip' => 1,
																	'Post.type_id' => $id2
																	),
											'order' => array('Post.created' => 'DESC'),
			));
			$this->set('data', $data);
		}
		//lay so tin hien thi tren mot trang
		$display=$this->requestAction(array("controller"=>"posts","action"=>"tinvip"));		
		//dem so tin
		$dem = $this->Post->find('count', array(
											'conditions' => array('catalog_id' => $id, 'Post.vip' => 0),											
		));
		if(isset($_GET['p']) && (int)$_GET['p']) {
                $p = $_GET['p'];
		}else{
			$p=1;
		}
		$this->set('p', $p);
		if($dem > $display) {
			$page = ceil($dem/$display);
		} else {
			$page = 1;
		}
		$this->set('page', $page);
		if(!$id2){
			$data2 = $this->Post->find('all', array(
											'conditions' => array('Post.catalog_id' => $id, 'Post.vip' => 0),
											'order' => array('Post.created' => 'DESC'),
											'limit' => $display,
											'page' => $p,
			));
			$this->set('data2', $data2);
		}
		else {
			$data2 = $this->Post->find('all', array(
											'conditions' => array('Post.catalog_id' => $id, 'Post.vip' => 0, 'Post.type_id' => $id2),
											'order' => array('Post.created' => 'DESC'),
											'limit' => $display,
											'page' => $p,
			));
			$this->set('data2', $data2);
		}
		//-------------Lay toan bo thong tin cua Type-----------
		$datatype = $this->Type->find('all');
		$this->set('datatype', $datatype);
		
	}

	function add() {
		$this->layout = 'admin_quanli';
		if (!empty($this->data)) {
			$this->Catalog->create();
			if ($this->Catalog->save($this->data)) {
				$this->Session->setFlash(__('The catalog has been saved', true));
				$this->redirect('/');
			} else {
				$this->Session->setFlash(__('The catalog could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		$this->layout = 'admin_quanli';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid catalog', true));
			$this->redirect(array('controller' => 'users','action' => 'quanli_catalog'));
		}
		if (!empty($this->data)) {
			if ($this->Catalog->save($this->data)) {
				
				$this->Session->setFlash(__('The catalog has been saved', true));
				$this->redirect(array('controller' => 'users','action' => 'quanli_catalog'));
			} else {
				$this->Session->setFlash(__('The catalog could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Catalog->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for catalog', true));
			$this->redirect(array('controller' => 'users','action' => 'quanli_catalog'));
		}
		if ($this->Catalog->delete($id)) {
			$data_posts = $this->Post->find('all', array(
											'conditions' => array(	'Post.catalog_id' => $id
																	),
											'order' => array('Post.created' => 'DESC'),
				));
				//----xoa het cac tin trong danh muc nay
			foreach ($data_posts as $data_post):
				$this->Post->delete($data_post['Post']['id']);
			endforeach;
			$this->Session->setFlash(__('Catalog deleted', true));
			$this->redirect(array('controller' => 'users','action' => 'quanli_catalog'));
		}
		$this->Session->setFlash(__('Catalog was not deleted', true));
		$this->redirect(array('controller' => 'users','action' => 'quanli_catalog'));
	}
	function exam01(){
		$data = $this->Catalogs->find("all");
		$this->set("data",$data);
	}
	function select(){
	$this->layout='trangchu';
	$datatt=$this->Catalog->find("all");
    return $datatt;
	
	}
	function demchuyenmuc(){
		$dem = $this->Catalog->find('count');
		return $dem;
	}
}
